import { socket } from "@kit.NetworkKit";
import { IllegalArgumentException } from "../Error/error";

export enum FamilyType {
  IPv4 = 1,
  IPv6 = 2,
  Domain = 3,
}
export class SocketAddress implements socket.NetAddress{
  address: string;
  family?: number;
  port?: number;
  constructor(address: string, port?: number, family?: FamilyType) {
    this.address = address;
    this.port = port;
    this.family = family;
  }
  static createUnresolved(var0: string,var1: number): SocketAddress {
    return new SocketAddress(checkHost(var0), checkPort(var1))
  }

  public equals(that: SocketAddress): boolean {
    return that instanceof SocketAddress && this.address === that.address && this.port === that.port && this.family === that.family;
  }

  public toString(): string {
    return `${this.address}:${this.port ?? 0}`
  }
}

function checkPort(var0: number): number {
  if (var0 >= 0 && var0 <= 65535) {
    return var0;
  } else {
    throw new IllegalArgumentException("port out of range:" + var0);
  }
}

function checkHost(var0: string): string {
  if (var0 === null || var0 === undefined) {
    throw new IllegalArgumentException("hostname can't be null");
  } else {
    return var0;
  }
}